package lightswitch.server.code.sqlhelper.expression.operators.relational;

import java.util.List;

import lightswitch.server.code.sqlhelper.ExpressionHelper;
import lightswitch.server.code.sqlhelper.expression.BinaryExpression;
import lightswitch.server.code.object.DataRow;
import lightswitch.server.code.sqlhelper.expression.Expression;
import lightswitch.server.code.sqlhelper.statement.select.From;
import lightswitch.server.code.util.TypeOfData;

public class GreaterThan extends BinaryExpression {

	public GreaterThan(Expression left, Expression right, boolean isNot)
	{
			this.leftExpression = left;
			this.rightExpression=right;
			this.isNot=isNot;
	}
	public GreaterThan(String left, String right, boolean isNot, From _from)
	{
				
			this.isNot=isNot;
			
			leftExpression = ExpressionHelper.parseExpression(left,_from);
			rightExpression = ExpressionHelper.parseExpression(right,_from);
			
	}
	public String toString()
	{
		if(isNot==false)
		return "("+leftExpression.toString() + " > " + rightExpression.toString() +" )";
		else
			return "("+leftExpression.toString() + " <= " + rightExpression.toString() +" )";
	}
	
	public Object GetValue(DataRow row, List<From> parentFromList) {
		dataType = TypeOfData.BOOLEAN;
		Object left = leftExpression.GetValue(row,parentFromList);
		Object right = rightExpression.GetValue(row,parentFromList);

		String type1 = leftExpression.dataType;
		String type2 = rightExpression.dataType;

		String type = "";
		if (type1.equals(type2)) {
			type = type1;

		} else {
			if ((type1.equals(TypeOfData.INT) && type2.equals(TypeOfData.FLOAT))
					|| (type1.equals(TypeOfData.INT) && type2.equals(TypeOfData.FLOAT))) {
				type = TypeOfData.FLOAT;

			}  else if ((type1.equals(TypeOfData.CHAR) || type2.equals(TypeOfData.CHAR))) {
				type = TypeOfData.CHAR;

			} else {
				type = TypeOfData.BOOLEAN;
			}
		}
		boolean res= false;
		switch(type)
		{
		case "int":
			res= Integer.parseInt(left.toString()) > Integer.parseInt(right.toString());
			break;
		case "float":
			res=  Float.parseFloat(left.toString()) > Float.parseFloat(right.toString());
			break;
		case "char":
			
			if(left.toString().compareTo(right.toString())>0)
				res = true;
			else
				res=false;
			break;
	
		default:
			//throw exception
			return null;
		
		}
		return (isNot==false?res:!res);
	}


}
